package com.iisigroup.log.statistics.internal;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;

import com.iisigroup.project.statistics.model.Func4Moi;
import com.iisigroup.project.statistics.model.Function;

public class Convert20150109 {
    private static final String expHibernateEntity="tw.gov.moi.domain..*Type";
    
    private static final  String expXldfDomainObject="tw.gov.moi.rl.component.xldf.dto..*";
    
    private static   void processJavaCodeList(final String[] classNames  ,final Set<String> javaCodes){
        if(ArrayUtils.isNotEmpty(classNames)){
            for(String classname : classNames){
                javaCodes.add(String.format("%s.java", classname));                
            }
        }
    } 
    public static  Func4Moi convertData(final Function srcData){
        final Func4Moi result = new Func4Moi ();
        
        
        /***
         * 填入作業代號
         * **/
        result.setSymbol(srcData.getFunctionName());
        
        /***
         * 填入作業名稱
         * **/
        result.setOperationName(srcData.getFunctionChtName());
        
        /***
         * 填入畫面
         * **/
        final List<String> jspFileNameList = new ArrayList<String>();
        for(File jsp :srcData.getJspFileList()){ 
            final String fileName = jsp.getName();
            jspFileNameList.add(fileName);
        }
        Collections.sort(jspFileNameList);
        result.setPages(jspFileNameList) ;
        
        /***
         * 填入程式代號
         * **/
        final Set<String> javaCodes = new HashSet<String>();
        String[] serviceNames = srcData.getServiceNames();
        String[] serviceImportedClassNames =  srcData.getServiceImportedClassNames();
        String[] controllerNames = srcData.getControllerNames();
        String[] controllerImportedClassNames = srcData.getControllerImportedClassNames();
        String obnfImplementedClass = srcData.getObnfImplementedClass();
        String[] obnfComponentImportedClassNames = srcData.getObnfComponentImportedClassNames();
        
        processJavaCodeList(serviceNames, javaCodes);
        processJavaCodeList(serviceImportedClassNames, javaCodes);
        processJavaCodeList(controllerNames, javaCodes);
        processJavaCodeList(controllerImportedClassNames, javaCodes);
        processJavaCodeList(obnfComponentImportedClassNames, javaCodes);
        
        javaCodes.add( obnfImplementedClass);
        List<String> javaCodeList = new ArrayList<String >();
        javaCodes.remove(null);
      
        final Iterator<String> javaCodeIterator = javaCodes.iterator();
        
        while(javaCodeIterator.hasNext()){
            final String code = javaCodeIterator.next();
            if(code.matches(expHibernateEntity) || code.matches(expXldfDomainObject)  ){
                javaCodeIterator.remove();
            }else if(StringUtils.contains(code, "tw.gov.moi")){                
                javaCodeList.add(StringUtils.substringAfterLast(StringUtils.removeEndIgnoreCase(code,".java"), ".")+".java");
            }else{
                javaCodeList.add( code);
            }
        }
        
        Collections.sort(javaCodeList  );
        result.setJavaCodes(javaCodeList);
        /***
         * 填入代碼檔(properties)
         * **/
        String displayRSCDs = srcData.getDisplayRSCDs();
        result.setProperties(spiltStr(displayRSCDs ));
        
        
        /***
         * 填入資料庫Table (含RSCD Table)
         * **/
        String displayTableName =   srcData.getDisplayTableName();       
        result.setTables(spiltStr(displayTableName ) ) ;
        
        /***
         * 填入檔案代號
         * **/
        String displayFtlsName = srcData.getDisplayFtlsName();
        result.setFilesSymbols(spiltStr (displayFtlsName ) ) ;
        
        return result ;
    }
    private static List<String> spiltStr(final String line ){
        final List<String> result =  new ArrayList<String>();
        final List<String> srcList = Arrays.asList(StringUtils.splitPreserveAllTokens(line, "\r\n"));
        for(String unitLine   : srcList){
            if(StringUtils.isNotBlank(unitLine)){
                result.add(unitLine);
            }
        }
        return result ; 
        
    }
}
